const datalist=require('./data/index')

const dayjs=require('dayjs')

const http =require('http')

//创服务器
const server=http.createServer()

//启动

server.listen(9000,()=>{
 console.log('欢迎哦');
})

server.on('request',(request,response)=>{
    response.setHeader('Content-Type', 'application/json; charset=utf-8')

  //解决跨域
    response.setHeader('Access-Control-Allow-Origin', '*')

    if(request.method === 'GET' && request.url === '/api/news'){

       const xinwen=datalist.news()
        xinwen.forEach(item => {
        item.publish_time=dayjs(item.publish_time).format('YYYY-MM-DD HH:mm:ss')
       });
       
       response.end(JSON.stringify(xinwen))
    }else if(request.method === 'GET' && request.url === '/api/students'){
        const stu=datalist.students()
        response.end(JSON.stringify(stu))
    }else if(request.method === 'GET' && request.url === '/api/lucystar'){
             const luy=datalist.lucystar()
        response.end(JSON.stringify(luy))

    }
    else{
        response.statusCode = 404
        response.end('404 not found')
    }

})